<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `0010101010110000101000010`

        
        

        let [l, mid, r] = [0, 1, 2]
        let max = 3
        let res = ''

        //合法标记
        let hefa = true

        while (r < input.length) {
            
            

            // 这两个符合完全连续方波,接着往下找
            if (input[mid] === '1' && input[r] === '0') {
                
                if ((r - l) % 2 === 1) {
                    hefa = false
                }
                mid++
                r++

                if(r===input.length){
                    if (r - l > max && hefa) {
                    max = r - l
                    res = input.slice(l, r)
                }
                }


            }

            // 这两个符合是否完全连续方波,接着往下找
            if (input[mid] === '0' && input[r] === '1') {
                if ((r - l) % 2 === 0) {
                    hefa = false
                    
                }
                mid++
                r++

            }



            //出现1 1不合法
            if (input[mid] === '1' && input[r] === '1') {
                mid++
                r++
                hefa = false
                continue
            }

            //出现0 0结束 计算结果，恢复默认标记
            if ((input[mid] === '0' && input[r] === '0')) {

                if (r - l > max && hefa) {
                    max = r - l
                    res = input.slice(l, r)
                    
                }
                l = r
                mid = r + 1
                r = l + 2
                hefa = true
                continue
            }


        }

        console.log(res);
        console.log(max);






    </script>
</body>

</html>